﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.Classify.Service;

public partial class View_CFG_CFG_CreditCardCount : ViewMatrix //System.Web.UI.Page
{
    string uploadFile = string.Empty;
    string ym = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        limit();
        Title = "彙整各卡別卡數統計";
        lblResult.Text = string.Empty;
    }

    protected void btnUPLoad_Click(object sender, EventArgs e)
    { 
        if(!saveAS(upLoadCreditCard))
        {return;}

        Squared_Excel seq_xls = new Squared_Excel();
        if (true == seq_xls.ReadUploadCreditCardCount(uploadFile, ym))
        {
            lblResult.Text = "上傳完成！";
        }
        else
        {
            lblResult.Text = "上傳失敗！";
        }
        seq_xls.ReleaseExcel();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {

        Service_CreditCardCount credit = new Service_CreditCardCount();
        //credit.GetSummaryData();


        string dlFile = Utility.TemplateToDownload(
            Server.MapPath("~"), "credit_card_count.xls", "xls");
        Squared_Excel seq_xls = new Squared_Excel();

        seq_xls.GetCreditCardCount(credit, dlFile);

        linkExcel.NavigateUrl = string.Format("~/download/{0}",
                System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
    }

    protected bool saveAS(System.Web.UI.WebControls.FileUpload upLoadCont)
    {
        bool ok = false; //沒有檔案就不往下做
        uploadFile = string.Empty;
        if (upLoadCont.HasFile)
        {
            uploadFile = Utility.SaveAS(upLoadCont, getResultValue(2)
            , Server.MapPath("~"));
            //彙整各卡別卡數統計10001，找出是年月的字串
            ym = upLoadCreditCard.FileName.Substring(14).Replace(".xls", "");
            ok = true;
        }
        return ok;
    }

}
